import 'package:extended_image/extended_image.dart';
import 'package:flutter/material.dart';
import 'package:loading_animation_widget/loading_animation_widget.dart';
import 'package:seo_renderer/seo_renderer.dart';

class AutoImage {
  static Widget buildImage(
    String uri, {
    BoxFit fit = BoxFit.cover,
    Color loadingColor = Colors.black,
    String alt = "",
  }) {
    if (uri.contains("http")) {
      return ImageRenderer(
        src: uri,
        child: ExtendedImage.network(
          uri,
          fit: fit,
          loadStateChanged: (state) {
            switch (state.extendedImageLoadState) {
              case LoadState.loading:
                return Center(
                  child: LoadingAnimationWidget.discreteCircle(
                    color: loadingColor,
                    size: 50,
                  ),
                );
              case LoadState.completed:
                return state.completedWidget;
              case LoadState.failed:
                return Center(
                  child: Icon(
                    Icons.error,
                    color: Colors.red,
                  ),
                );
            }
            return null;
          },
        ),
        alt: alt,
      );
    } else {
      return ImageRenderer(
        src: uri,
        child: ExtendedImage.asset(
          uri,
          fit: fit,
          loadStateChanged: (state) {
            switch (state.extendedImageLoadState) {
              case LoadState.loading:
                return Center(
                  child: LoadingAnimationWidget.discreteCircle(
                    color: loadingColor,
                    size: 50,
                  ),
                );
              case LoadState.completed:
                return state.completedWidget;
              case LoadState.failed:
                return Center(
                  child: Icon(
                    Icons.error,
                    color: Colors.red,
                  ),
                );
            }
            return null;
          },
        ),
        alt: alt,
      );
    }
  }

  static ImageProvider buildImageProvider(String uri) {
    if (uri.contains("http")) {
      return ExtendedNetworkImageProvider(
        uri,
        cache: true,
      );
    } else {
      return AssetImage(uri);
    }
  }
}
